package com.tutor.lzm.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.app.Constant;
import com.tutor.lzm.bean.TeacherDetailInfo;
import com.tutor.lzm.bean.TeacherExperienceBean;
import com.tutor.lzm.function.teacher.act.TeacherExperienceListAct;
import com.tutor.lzm.function.teacher.adapter.TeacherExperienceAdapter;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.CancelSelectedTeacherRequest;
import com.tutor.lzm.http.request.CancelTeacherCollectionRequest;
import com.tutor.lzm.http.request.SelectedTeacherRequest;
import com.tutor.lzm.http.request.TeacherCollectionRequest;
import com.tutor.lzm.http.request.TeacherDetailInfoRequest;
import com.tutor.lzm.view.RoundImageView;
import com.tutor.syslib.Logs;
import com.tutor.syslib.util.MessageTools;
import com.tutor.syslib.util.OtherUtil;
import com.tutor.syslib.util.TextUtil;
import com.tutor.syslib.view.MyListView;
import com.tutor.syslib.view.RichTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zzgpc on 2017-9-17.
 * 老师详情
 */
public class TeacherDetailsAct extends BaseActivity {


    @BindView(R.id.img_teacher)
    RoundImageView imgTeacher;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_teach_level)
    TextView tvTeachLevel;
    @BindView(R.id.ratingBar)
    RatingBar ratingBar;
    @BindView(R.id.tv_teach_price)
    TextView tvTeachPrice;
    @BindView(R.id.tv_real_name)
    TextView tvRealName;
    @BindView(R.id.tv_education)
    TextView tvEducation;
    @BindView(R.id.tv_qualification)
    TextView tvQualification;
    @BindView(R.id.tv_te_title_flag)
    TextView tvTeTitleFlag;
    @BindView(R.id.tv_teacher_features_more)
    TextView tvTeacherFeaturesMore;
    @BindView(R.id.img_teacher_features_zhankai)
    ImageView imgTeacherFeaturesZhankai;
    @BindView(R.id.teaching_features_line)
    View teachingFeaturesLine;
    @BindView(R.id.tv_teaching_features)
    TextView tvTeachingFeatures;
    @BindView(R.id.tv_experience_num)
    TextView tvExperienceNum;
    @BindView(R.id.img_experience_more)
    ImageView imgExperienceMore;
    @BindView(R.id.listView_experience)
    MyListView listViewExperience;
    @BindView(R.id.tv_education_null)
    TextView tvEducationNull;
    @BindView(R.id.tv_collection)
    RichTextView tvCollection;
    @BindView(R.id.rl_collection)
    RelativeLayout rlCollection;
    @BindView(R.id.tv_collection_cancel)
    RichTextView tvCollectionCancel;
    @BindView(R.id.rl_collection_cancel)
    RelativeLayout rlCollectionCancel;
    @BindView(R.id.tv_select_teacher)
    RichTextView tvSelectTeacher;
    @BindView(R.id.rl_select_teacher)
    RelativeLayout rlSelectTeacher;
    @BindView(R.id.tv_call_teacher)
    RichTextView tvCallTeacher;
    @BindView(R.id.rl_call_teacher)
    RelativeLayout rlCallTeacher;
    @BindView(R.id.tv_title_left)
    TextView tvTitleLeft;
    @BindView(R.id.tv_teacher_features_flag)
    TextView tvTeacherFeaturesFlag;
    @BindView(R.id.tv_education_experience_flag)
    TextView tvEducationExperienceFlag;
    private TeacherDetailInfo info;
    private String teacherId;


    /**
     * 用于取消 选定老师
     */
    private String applyId;

    /**
     * 是否是从家长的 意向进来，选择老师的
     */
    private int selectStatus = -1;


    public static void start(Context context, String teacherId) {
        Intent intent = new Intent(context, TeacherDetailsAct.class);
        intent.putExtra("teacherId", teacherId);
        context.startActivity(intent);
    }


    public static void start(Context context, String teacherId, int selectStatus) {
        Intent intent = new Intent(context, TeacherDetailsAct.class);
        intent.putExtra("teacherId", teacherId);
        intent.putExtra("isSelect", selectStatus);
        context.startActivity(intent);
    }

    public static void start(Context context, String teacherId, int selectStatus, String applyId) {
        Intent intent = new Intent(context, TeacherDetailsAct.class);
        intent.putExtra("teacherId", teacherId);
        intent.putExtra("isSelect", selectStatus);
        intent.putExtra("applyId", applyId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_details);
        ButterKnife.bind(this);
        initTitle("老师详情", "分享", rightListener);
        teacherId = getIntent().getStringExtra("teacherId");
        if (getIntent().hasExtra("isSelect")) {
            selectStatus = getIntent().getIntExtra("isSelect", -1);
        }
        if (getIntent().hasExtra("applyId")) {
            applyId = getIntent().getStringExtra("applyId");
        }

        initData();
    }


    private void initData() {
        final TeacherDetailInfoRequest request = new TeacherDetailInfoRequest();
        request.setTeacherId(teacherId);
        request.setUserId(AppSession.getInstance().getUserId());
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                info = (TeacherDetailInfo) response;
                if (info == null) {
                    return;
                }
                initView();
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);
            }
        });

    }


    private void initView() {
        imgTeacherFeaturesZhankai.setFocusable(true);
        imgTeacherFeaturesZhankai.setFocusableInTouchMode(true);
        imgTeacherFeaturesZhankai.requestFocus();
        listViewExperience.setFocusable(false);
        Picasso.with(this)
                .load(info.headImage)
                .into(imgTeacher);
        tvTitle.setText(info.name);
        tvTeachLevel.setText(info.gradeIdText + info.categoryIdText + "  教龄" + info.workingAge + "年");
        ratingBar.setRating(5);
        tvTeachPrice.setText(info.price + "元/小时起");
        Drawable drawable = getResources().getDrawable(R.mipmap.gouxuan);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), (int) (drawable.getMinimumHeight()));
        if (info.isAuthentication == 1) {
            tvRealName.setCompoundDrawables(drawable, null, null, null);
        }

        if (info.isDegree == 1) {
            tvEducation.setCompoundDrawables(drawable, null, null, null);
        }
        if (info.isTeacherCertification == 1) {
            tvQualification.setCompoundDrawables(drawable, null, null, null);
        }

        tvTeachingFeatures.setText(info.characteristic);

        if (tvTeachingFeatures.getLineCount() <= 4) {
            tvTeacherFeaturesMore.setVisibility(View.GONE);
            imgTeacherFeaturesZhankai.setVisibility(View.GONE);

        }
        List<TeacherExperienceBean> seniorityList = info.seniorityList;
        if (seniorityList != null && seniorityList.size() > 0) {
            tvExperienceNum.setText("共" + seniorityList.size() + "条");
            TeacherExperienceAdapter adapter = new TeacherExperienceAdapter(this, seniorityList, false);
            listViewExperience.setAdapter(adapter);

        } else {
            tvExperienceNum.setText("共0条");
            tvEducationNull.setVisibility(View.VISIBLE);
            tvEducationNull.setText("老师还未留下教育经历记录");
        }

        /**
         * 是否关注
         */
        if (TextUtil.isNull(info.collectId)) {
            rlSelectTeacher.setVisibility(View.GONE);
            rlCollection.setVisibility(View.VISIBLE);

        } else {
            rlSelectTeacher.setVisibility(View.GONE);
            rlCollection.setVisibility(View.GONE);
            rlCollectionCancel.setVisibility(View.VISIBLE);
        }

        /**
         * 是否已经选定老师了
         */
        if (selectStatus != -1) {
            rlCollectionCancel.setVisibility(View.GONE);
            rlCollection.setVisibility(View.GONE);
            rlSelectTeacher.setVisibility(View.VISIBLE);
            if (selectStatus == Constant.teacher_un_select) {
                //未选定
                tvSelectTeacher.setText("选定老师");
            } else if (selectStatus == Constant.teacher_is_select) {
                //已选定
                if (TextUtil.isNull(applyId)) {
                    tvSelectTeacher.setText("已经选定");
                }
                tvSelectTeacher.setText("取消选定");

            }
        }


    }

    private String mNames[] = {
            "培养学习习惯", "培养学习习惯", "培养学习习惯",
            "耐心"};

    /**
     * 加载老师特点标签
     */
//    private void initTeacherCharacteristicsViews() {
//        // TODO Auto-generated method stub
//        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
//                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        lp.leftMargin = 5;
//        lp.rightMargin = 5;
//        lp.topMargin = 5;
//        lp.bottomMargin = 5;
//        for (int i = 0; i < mNames.length; i++) {
//            TextView view = new TextView(this);
//            view.setText(mNames[i]);
//            view.setTextColor(getResources().getColor(R.color.text_gray));
//            view.setTextSize(TypedValue.COMPLEX_UNIT_SP, getResources().getDimension(R.dimen.flowlayout_textSize));
////            view.setTextSize(getResources().getDimension(R.dimen.flowlayout_textSize));
//            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.flowlayout_textview));
////            flowlayout2.addView(view, lp);
//            flowlayout.addView(view, lp);
//        }
//    }
    @OnClick({R.id.img_teacher_features_zhankai, R.id.tv_teacher_features_more,
            R.id.rl_collection, R.id.rl_call_teacher, R.id.rl_collection_cancel
            , R.id.rl_select_teacher})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_teacher_features_zhankai:
            case R.id.tv_teacher_features_more:
                //展示教学特点全文

                String teacherFeaturesFlag = tvTeacherFeaturesMore.getText().toString();
                if (teacherFeaturesFlag.equals("展开")) {
                    tvTeachingFeatures.setMaxLines(Integer.MAX_VALUE);
                    imgTeacherFeaturesZhankai.setBackground(getResources().getDrawable(R.mipmap.arrow_top));
                    tvTeacherFeaturesMore.setText("收起");
                } else {
                    tvTeachingFeatures.setMaxLines(4);
                    imgTeacherFeaturesZhankai.setBackground(getResources().getDrawable(R.mipmap.arrow_down));
                    tvTeacherFeaturesMore.setText("展开");
                }


                break;
//            case R.id.tv_experience_num:
//            case R.id.img_experience_more:
//                //展示更多教育经历
////                if (info.seniorityList == null || info.seniorityList.size() < 1) {
////                    return;
////                }
//                TeacherExperienceListAct.start(this, teacherId);
//                break;

            case R.id.rl_collection:
                //收藏
                collectionTeacher();
                break;
            case R.id.rl_collection_cancel:
                //取消收藏
                cancelCollectionTeacher();
                break;
            case R.id.rl_select_teacher:
                if (TextUtil.isNull(applyId)) {
                    MessageTools.showDialogOk(oThis, "已经确定的老师无法取消");
                }

                if (selectStatus == Constant.teacher_is_select) {
                    //取消选定老师
                    cancelSelectedTeacher();
                } else {
                    //选定老师
                    selectedTeacher();
                }
                break;
            case R.id.rl_call_teacher:
                //联系老师
                OtherUtil.makeCallView(TeacherDetailsAct.this, info.phone);
                break;
        }
    }


    /**
     * 选定老师
     */
    private void selectedTeacher() {
        final SelectedTeacherRequest request = new SelectedTeacherRequest();
        request.applyId = applyId;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                tvSelectTeacher.setText("取消选定");
                selectStatus = Constant.teacher_is_select;
                MessageTools.showToast(getApplicationContext(), "选定老师成功...");
                Logs.d(tag, "选定成功");
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);
            }
        });
    }

    /**
     * q取消选定老师
     */
    private void cancelSelectedTeacher() {
        final CancelSelectedTeacherRequest request = new CancelSelectedTeacherRequest();
        request.applyId = applyId;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                tvSelectTeacher.setText("选定老师");
                selectStatus = Constant.teacher_is_select;
                Logs.d(tag, "选定成功");
                MessageTools.showToast(getApplicationContext(), "取消老师成功...");
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);
            }
        });
    }

    /**
     * 点击收藏
     */
    private void collectionTeacher() {
        final TeacherCollectionRequest request = new TeacherCollectionRequest();
        request.setTeacherId(teacherId);
        request.setUserId(AppSession.getInstance().getUserId());
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                info.collectId = "1";
                rlCollection.setVisibility(View.GONE);
                rlCollectionCancel.setVisibility(View.VISIBLE);
                Logs.d(tag, "收藏成功");
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "getRecommendSubject    code" + code + "      msg=" + msg);
            }
        });
    }

    /**
     * 点击取消收藏
     */
    private void cancelCollectionTeacher() {
        final CancelTeacherCollectionRequest request = new CancelTeacherCollectionRequest();
        request.setTeacherId(teacherId);
        request.setUserId(AppSession.getInstance().getUserId());
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                info.collectId = "";
                rlCollection.setVisibility(View.VISIBLE);
                rlCollectionCancel.setVisibility(View.GONE);
                Logs.d(tag, "取消收藏成功");
                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d(tag, "cancelCollectionTeacher    code" + code + "      msg=" + msg);
            }
        });
    }

    private View.OnClickListener rightListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    };
}
